package com.wang.tx.core;

// 事务单位元
public abstract class Tx {
    protected abstract void prepare() throws Throwable;

    protected abstract boolean execute() throws Throwable;

    // 为保证事务能正常结束，要求回滚和提交是不可以出现异常的，如有异常由用户去处理。
    protected abstract void commit();
    protected abstract void rollback();

    public void run() {
        try {
            this.prepare();
        } catch (Throwable e) {
            e.printStackTrace();
            this.rollback();
        }
        try{
            if (this.execute()) {
                this.commit();
            } else {
                this.rollback();
            }
        } catch (Throwable e) {
            e.printStackTrace();
            this.rollback();
        }
    }
}
